home *** CD-ROM | disk | FTP | other *** search
/ T&A 2 the Maxx 3 / T and A 2 The Maxx Number 3.iso / viewers / unixview / xgiftar.z / xgiftar / path.c < prev    next >
C/C++ Source or Header  |  1991-05-20  |  6KB  |  314 lines

  1. /* path.c:
  2.  *
  3.  * functions that deal with the image path
  4.  *
  5.  * jim frost 10.03.89
  6.  *
  7.  * Copyright 1989, 1990, 1991 Jim Frost.
  8.  * See included file "copyright.h" for complete copyright information.
  9.  */
  10.  
  11. #include "copyright.h"
  12. #include "xloadimage.h"
  13. #include <X11/Xos.h>
  14. #include <sys/stat.h>
  15. #include <ctype.h>
  16. #ifndef VMS
  17. #include <pwd.h>
  18. #endif
  19. #include <errno.h>
  20. #ifdef SYSV
  21. #include <unistd.h>
  22. #endif
  23.  
  24. /* SUPPRESS 530 */
  25. /* SUPPRESS 560 */
  26.  
  27. extern int errno;
  28.  
  29. static unsigned int  NumPaths= 0;
  30. static unsigned int  NumExts= 0;
  31. static char         *Paths[BUFSIZ];
  32. static char         *Exts[BUFSIZ];
  33. static char         *PathToken= "path=";
  34. static char         *ExtToken= "extension=";
  35.  
  36. #define VOIDSECTION 0
  37. #define PATHSECTION 1
  38. #define EXTSECTION  2
  39.  
  40. static void readPathsAndExts(name)
  41.      char *name;
  42. { FILE         *f;
  43.   char          tokenbuf[BUFSIZ];
  44.   char          buf[BUFSIZ];
  45.   unsigned int  secnum;
  46.   unsigned int  linenum;
  47.   unsigned int  a, b, l;
  48.   int           c;
  49.  
  50.   if (! (f= fopen(name, "r")))
  51.     return;
  52.  
  53.   secnum= VOIDSECTION;
  54.   linenum= 0;
  55.   while (fscanf(f, "%s", tokenbuf) > 0) {
  56.     linenum++;
  57.     l= strlen(tokenbuf);
  58.     for (a= 0, b= 0; a < l; a++, b++) {
  59.       if (tokenbuf[a] == '\\')
  60.     tokenbuf[b]= tokenbuf[++a];
  61.       else if (b != a)
  62.     tokenbuf[b]= tokenbuf[a];
  63.       if (tokenbuf[a] == '#') {
  64.     tokenbuf[b]= '\0';
  65.     while (((c= fgetc(f)) != '\n') && (c != EOF))
  66.       ;
  67.     break;
  68.       }
  69.     }
  70.  
  71.     if (!strncmp(tokenbuf, PathToken, strlen(PathToken))) {
  72.       secnum= PATHSECTION;
  73.       if (sscanf(tokenbuf + strlen(PathToken), "%s", buf) != 1)
  74.     continue;
  75.     }
  76.     else if (!strncmp(tokenbuf, ExtToken, strlen(ExtToken))) {
  77.       secnum= EXTSECTION;
  78.       if (sscanf(tokenbuf + strlen(ExtToken), "%s", buf) != 1)
  79.     continue;
  80.     }
  81.     else
  82.       strcpy(buf, tokenbuf);
  83.     if (buf[0] == '\0')
  84.       continue;
  85.  
  86.     switch (secnum) {
  87.     case VOIDSECTION:
  88.       printf("%s: %d: Syntax error\n", name, linenum); /* ala BASIC */
  89.       fclose(f);
  90.       return;
  91.     case PATHSECTION:
  92.       if (NumPaths < BUFSIZ - 1)
  93.     Paths[NumPaths++]= expandPath(buf);
  94.       else {
  95.     printf("%s: %d: Path table overflow\n", name, linenum);
  96.     fclose(f);
  97.     return;
  98.       }
  99.       break;
  100.     case EXTSECTION:
  101.       if (NumExts < BUFSIZ - 1)
  102.     Exts[NumExts++]= dupString(buf);
  103.       else {
  104.     printf("%s: %d: Extension table overflow\n", name, linenum);
  105.     fclose(f);
  106.       }
  107.       break;
  108.     }
  109.   }
  110. }
  111.  
  112. void loadPathsAndExts()
  113. { static int     havepaths= 0;
  114. #ifndef VMS
  115.   struct passwd *pw;
  116. #endif
  117.   char           buf[BUFSIZ];
  118.  
  119.   if (havepaths)
  120.     return;
  121.   havepaths= 1;
  122.  
  123. #ifdef VMS
  124.   sprintf(buf, "/sys$scratch/.xloadimagerc");
  125. #else /* !VMS */
  126.   if (! (pw= (struct passwd *)getpwuid(getuid()))) {
  127.     printf("Can't find your password file entry?!?\n");
  128.     return;
  129.   }
  130.   sprintf(buf, "%s/.xloadimagerc", pw->pw_dir);
  131. #endif /* !VMS */
  132.   if (! access(buf, R_OK)) {
  133.     readPathsAndExts(buf);
  134.     return; /* don't read system file if user has one */
  135.   }
  136. #ifdef SYSPATHFILE
  137.   readPathsAndExts(SYSPATHFILE);
  138. #endif
  139. }
  140.  
  141. static int fileIsOk(fullname, sbuf)
  142.      char *fullname;
  143.      struct stat *sbuf;
  144. {
  145.   if ((sbuf->st_mode & S_IFMT) == S_IFDIR) /* is a directory */
  146.     return(0);
  147.   return(access(fullname, R_OK)); /* we can read it */
  148. }
  149.  
  150. /* find an image with paths and extensions from defaults files.  returns
  151.  * -1 if access denied or not found, 0 if ok.
  152.  */
  153.  
  154. int findImage(name, fullname)
  155.      char *name, *fullname;
  156. { unsigned int   p, e;
  157.   struct stat    sbuf;
  158.  
  159.   strcpy(fullname, name);
  160.   if (!strcmp(name, "stdin")) /* stdin is special name */
  161.     return(0);
  162.  
  163.   /* look for name and name with compress extension
  164.    */
  165.   if (! stat(fullname, &sbuf))
  166.       return(fileIsOk(fullname, &sbuf));
  167. #ifndef NO_COMPRESS
  168.   strcat(fullname, ".Z");
  169.   if (! stat(fullname, &sbuf))
  170.       return(fileIsOk(fullname, &sbuf));
  171. #endif
  172.  
  173.   for (p= 0; p < NumPaths; p++) {
  174.     sprintf(fullname, "%s/%s", Paths[p], name);
  175.     if (! stat(fullname, &sbuf))
  176.       return(fileIsOk(fullname, &sbuf));
  177. #ifndef NO_COMPRESS
  178.     strcat(fullname, ".Z");
  179.     if (! stat(fullname, &sbuf))
  180. #endif
  181.       return(fileIsOk(fullname, &sbuf));
  182.     for (e= 0; e < NumExts; e++) {
  183.       sprintf(fullname, "%s/%s%s", Paths[p], name, Exts[e]);
  184.       if (! stat(fullname, &sbuf))
  185.     return(fileIsOk(fullname, &sbuf));
  186. #ifndef NO_COMPRESS
  187.       strcat(fullname, ".Z");
  188.       if (! stat(fullname, &sbuf))
  189.     return(fileIsOk(fullname, &sbuf));
  190. #endif
  191.     }
  192.   }
  193.  
  194.   for (e= 0; e < NumExts; e++) {
  195.     sprintf(fullname, "%s%s", name, Exts[e]);
  196.     if (! stat(fullname, &sbuf))
  197.       return(fileIsOk(fullname, &sbuf));
  198. #ifndef NO_COMPRESS
  199.     strcat(fullname, ".Z");
  200.     if (! stat(fullname, &sbuf))
  201.       return(fileIsOk(fullname, &sbuf));
  202. #endif
  203.   }
  204.   errno= ENOENT; /* file not found */
  205.   return(-1);
  206. }
  207.  
  208. /* list images along our path
  209.  */
  210.  
  211. void listImages()
  212. { unsigned int a;
  213.   char         buf[BUFSIZ];
  214.  
  215.   if (!NumPaths) {
  216.     printf("No image path\n");
  217.     return;
  218.   }
  219.   for (a= 0; a < NumPaths; a++) {
  220.     printf("%s:\n", Paths[a]);
  221.     fflush(stdout);
  222.     sprintf(buf, "ls %s", Paths[a]);
  223.     if (system(buf) < 0) {
  224.       perror("ls");
  225.       return;
  226.     }
  227.   }
  228.   return;
  229. }
  230.  
  231. void showPath()
  232. { int a;
  233.  
  234.   if (!NumPaths && !NumExts) {
  235.     printf("No image paths or extensions\n");
  236.     return;
  237.   }
  238.   if (NumPaths) {
  239.     printf("Image path:");
  240.     for (a= 0; a < NumPaths; a++)
  241.       printf(" %s", Paths[a]);
  242.     printf("\n");
  243.   }
  244.   if (NumExts) {
  245.     printf("Image extensions:");
  246.     for (a= 0; a < NumExts; a++)
  247.       printf(" %s", Exts[a]);
  248.     printf("\n");
  249.   }
  250. }
  251.  
  252. char *expandPath(p)
  253.      char *p;
  254. { char buf1[BUFSIZ], buf2[BUFSIZ];
  255.   int b1, b2, var;
  256.   char *ptr;
  257.  
  258.   char *getenv();
  259.  
  260.   buf1[0] = '\0';
  261.   buf2[0] = '\0';
  262.   b1 = 0;
  263.   b2 = 0;
  264.   var = 0;
  265.  
  266.   while(*p) {
  267.     if(isspace(*p)) break;
  268.     if (*p == '$') var++;
  269.     else if(*p == '~') {
  270.       buf1[b1] = '\0';
  271.       strcat(buf1, getenv("HOME"));
  272.       b1 = strlen(buf1);
  273.       var = 0;
  274.     }
  275.     else if(*p == '/' || *p == '}') {
  276.       if(var) {
  277.     buf1[b1] = '\0';
  278.     buf2[b2] = '\0';
  279.     strcat(buf1, getenv(buf2));
  280.     b1 = strlen(buf1);
  281.     buf2[0] = '\0';
  282.     b2 = 0;
  283.     var = 0;
  284.       }
  285.       if(*p == '/') {
  286.     buf1[b1] = *p;
  287.     b1++;
  288.       }
  289.     }
  290.     else if(var) {
  291.       if(*p != '{') {
  292.     buf2[b2] = *p;
  293.     b2++;
  294.       }
  295.     }
  296.     else {
  297.       buf1[b1] = *p;
  298.       b1++;
  299.     }
  300.     p++;
  301.   }
  302.  
  303.   buf1[b1] = '\0';
  304.   
  305.   if((b2 = strlen(buf1)) > 0) {
  306.     ptr = (char *)lmalloc((unsigned) b2+1);
  307.     strcpy(ptr, buf1);
  308.     return(ptr);
  309.   }
  310.   else
  311.     return(NULL);
  312.  
  313. }
  314.